package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.JoinPageSignUpForm;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Join page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class JoinPage extends LoadableComponent<JoinPage> implements IPage {

    // ************************ Locators ************************

    public static final String JOIN_PAGE_URL = "https://opensky.com/join";

    public static final String LOGO = "//img[@id='oslogo']";

    public static final String LOGO_LINK = "//div[@class='logo']//a";

    // ************************ Complex page objects **************************

    private JoinPageSignUpForm signUpForm;

    private final WebDriverWrapper driver;

    public JoinPage(WebDriverWrapper driver) {
	this.driver = driver;
	signUpForm = new JoinPageSignUpForm(driver);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(JOIN_PAGE_URL);
    }

    public HomePage clickLogo() {

	HomePage homePage = new HomePage(driver);

	driver.switchTo().defaultContent();
	driver.waitAndClick("LOGO_LINK", By.xpath(LOGO_LINK));

	return homePage.isPageLoaded() ? homePage : null;
    }

    // ************************ Validations **************************
    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(JOIN_PAGE_URL, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(JOIN_PAGE_URL);
    }

    public boolean isLogoPresent() {
	return driver.waitAndVerifyElementPresent("LOGO", By.xpath(LOGO));
    }

    // ************************ GET/SET methods **************************

    public JoinPageSignUpForm getSignUpForm() {
	return signUpForm.isComponentLoaded() ? signUpForm : null;
    }

    @Override
    public String getName() {
	return "Join page";
    }
}
